axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net';

// 添加拦截器===>拦截请求
axios.interceptors.request.use(function (config) {
    console.log(config);
    if (config.url.indexOf('/my') != -1) {
        config.headers.Authorization = localStorage.getItem('token')
    }
    return config; //修改完config对象return返回
}, function (error) {
    return Promise.reject(error)
})

// 添加响应拦截
axios.interceptors.response.use(function (response) {
    if (response.data.message !== '获取用户基本信息成功！') {
        //   跳转到登录页面,删除本地存储里的token键
        location.href = '/login.html';
        localStorage.removeItem('token')
    }
    return response;
}, function (error) {
    return Promise.reject(error)
})